package api.Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo {

    public static void main(String[] args) {
        /**
         * 使用Collection集合三个学生对象,并遍历
         * <p>
         * 创建学生类
         * 创建三个学生对象
         * 创建集合对象
         * 把对象添加到集合中
         * 遍历
         */
        Student s1 = new Student("张三", 22);
        Student s2 = new Student("蔡徐坤", 32);
        Student s3 = new Student("小老鼠", 44);

        //创建集合
        Collection<Student> stu = new ArrayList<Student>();

        //把对象添加到集合中
        stu.add(s1);
        stu.add(s2);
        stu.add(s3);

        //增强for循环遍历
        for(Student student:stu){
            System.out.println(student.getName()+student.getAge());
        }
        System.out.println("------------------------");

        //迭代器遍历

        Iterator<Student> iterator = stu.iterator();


        //返回下一个迭代器
        //判断条件
        while (iterator.hasNext()){
            Student next = iterator.next();
            System.out.println(next.getName()+next.getAge());
        }

        System.out.println("--------------------");

        stu.forEach(student -> System.out.println(student.getName()+"  "+student.getAge()));


    }


}
